package com.emagic.manage.common.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.emagic.manage.R;
import com.chad.library.adapter.base.BaseQuickAdapter;

public abstract class BaseListFragment extends BaseFragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int setupContentView() {
        return R.layout.fragment_common_recycler_view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView = $(view, R.id.recycler_view);
        mSwipeRefreshLayout = $(view, R.id.swipe_refresh_layout);

        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
        setupRecyclerView(mRecyclerView);
    }


    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return mSwipeRefreshLayout;
    }

    protected void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
        mSwipeRefreshLayout.setOnRefreshListener(listener);
    }

    protected void setRefreshing(boolean b) {
        mSwipeRefreshLayout.setRefreshing(b);
    }

    protected void setEnableLoadMore(boolean enable) {
        if (mRecyclerView.getAdapter() != null
                && mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).setEnableLoadMore(enable);
        }
    }

    protected void showNoMoreData() {
        if (mRecyclerView.getAdapter() != null
                && mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreEnd();
        }
    }

    protected void showLoadMoreFail() {
        if (mRecyclerView.getAdapter() != null
                && mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreFail();
        }
    }

    protected void loadMoreComplete() {
        if (mRecyclerView.getAdapter() != null
                && mRecyclerView.getAdapter() instanceof BaseQuickAdapter) {
            ((BaseQuickAdapter) mRecyclerView.getAdapter()).loadMoreComplete();
        }
    }

    protected abstract void setupRecyclerView(RecyclerView recyclerView);
}
